/*选择房间*/


define(function (require, exports, module) {
	var HandlerBars = require("handlebars");
	var typeTpl = require("../../../roompanorama/src/assets/tpl/roompanorama_type.tpl");
	var roomTpl = require("../../../roompanorama/src/assets/tpl/roompanorama_room.tpl");
	require("../../../roompanorama/src/assets/css/roompanorama_main.css")
	var roomPanoramaEvents = require("../../../roompanorama/src/handler/events");
	var scatterCheckInBiz = require("../business/scatterCheckInBiz");
	var ScatterCheckInFormEnd = require("../component/ScatterCheckInFormEnd");
	
    module.exports = {
    	showRooms:function(widget){
    		var html = HandlerBars.compile(typeTpl)();
    		$(".J-status",widget.element).html(html);
    		var tempGridRowData = widget.get("tempGridRowData");
    		
    		var roomRegister = {};
    		var editGridData = widget.get("editgrid").getData();
    		for(var i=0;i<editGridData.length;i++){
    			var room = editGridData[i].roomRegister.room;
    			var personalInfo = editGridData[i].nonMemberRegister.signUpRegister.personalInfo;
    			if(room && room.pkRoom){
    				if(!roomRegister[room.pkRoom]){
    					roomRegister[room.pkRoom]={};
    					roomRegister[room.pkRoom]["room"] = room;
        				roomRegister[room.pkRoom]["registerPersonInfos"] = [];
        				roomRegister[room.pkRoom]["registerPersonInfos"].push(personalInfo);
    				}else{
    					roomRegister[room.pkRoom]["registerPersonInfos"].push(personalInfo);
    				}
    			}
    		}
    		widget.set("roomRegister",roomRegister);
    		
    	},
    	events:function(){
    		return roomPanoramaEvents.events;
    	},
    	changeImg:roomPanoramaEvents.changeImg,
        print:roomPanoramaEvents.print,
        setData:roomPanoramaEvents.setData,
        setRoomPersonInfo:roomPanoramaEvents.setRoomPersonInfo,
        renderRoomNumber:roomPanoramaEvents.renderRoomNumber,
        selectPersonInfo:roomPanoramaEvents.selectPersonInfo,
        //房屋选择确认
        roomStateConfirm:function(){
        	var form ={};
    		if(this.get("showForm")=='.J-form'){
    			form = this.get("form");
    		}else{
    			form =this.get("doorForm");
    		}
        	var pkCIImplement = form.getValue("pkCIImplement");
        	var sourceType = form.getValue("sourceType");
        	var signUpType = form.getValue("signUpType");
        	var isVerify = false;
        	var pkOrgBuildingIn = [];
        	if(pkCIImplement){
        		if(signUpType == "NonMember" && sourceType == "DropIn"){
        			isVerify = false;
        		}else{
        			isVerify = false;
        			if(signUpType == "Experience"){
        				isVerify = true;
        			}
        		}
        	}else{
        		isVerify = false;
        	}
        	
        	var roomRegister = this.get("roomRegister");
        	var personInfoList = [];
        	for(var key in roomRegister){
        		for(var i=0;i<roomRegister[key]["registerPersonInfos"].length;i++){
        			personInfoList.push({
        				room:roomRegister[key]["room"],
        				personInfo:roomRegister[key]["registerPersonInfos"][i]
        			});
        		}
        	}
        	var editGridData = this.get("editgrid").getData();
        	
        	this.set("roomRegister",null);
        	
        	if(personInfoList.length>0){
        		if(isVerify && editGridData.length>0){
        			for(var j=0;j<editGridData.length;j++){
        				var pkPersonalInfo = editGridData[j].nonMemberRegister.signUpRegister.personalInfo.pkPersonalInfo;
        				for(var i=0;i<personInfoList.length;i++){
            				if(pkPersonalInfo == personInfoList[i].personInfo.pkPersonalInfo){
            					editGridData[j].roomRegister.room = personInfoList[i].room;
            					editGridData[j].checkIn = true;
            					break;
            				}
            			}
        			}
        		}else{
        			editGridData = [];
        			for(var i=0;i<personInfoList.length;i++){
        				var roomPersonalInfo = {
        						roomRegister:{
        							room:personInfoList[i].room
        						},
        						checkIn:true,
        						nonMemberRegister:{
        							signUpRegister:{
        								personalInfo:personInfoList[i].personInfo,
        								member:personInfoList[i].personInfo.member,
        								version:0
        							},
        							version:0
        						}
        				};
        				editGridData.push(roomPersonalInfo);
        			}
        		}
        		
        		this.get("editgrid").setData(editGridData);
        		this.get("form").setValue("signUpNum",editGridData.length);
        		var roomTypesObj = {};
        		var pkRoomTypeIn = [];
        		for(var i=0;i<editGridData.length;i++){
        			if(editGridData[i].roomRegister.room){
        				roomTypesObj[editGridData[i].roomRegister.room.roomType.pkRoomType] = editGridData[i].roomRegister.room.roomType;
        				editGridData[i].roomRegister.room.orgBuilding = {pkOrgBuilding:editGridData[i].roomRegister.room.pkOrgBuilding};
        				pkOrgBuildingIn.push(editGridData[i].roomRegister.room.pkOrgBuilding);
        			}
        		}
        		
        		for(var key in roomTypesObj){
        			pkRoomTypeIn.push(key);
    			}
        		
        		var tempGridRowData = this.get("tempGridRowData");
        		var signUpRooms = [];
        		if(tempGridRowData){
        			signUpRooms = tempGridRowData.nonMemGroupReserve.signUpRooms;
        		}
        		if(signUpRooms && signUpRooms.length>0){
        			for(var i=0;i<signUpRooms.length;i++){
        				if(roomTypesObj[signUpRooms[i].roomType.pkRoomType]){
        					delete roomTypesObj[signUpRooms[i].roomType.pkRoomType];
        				}
        			}
        		}else{
        			for(var key in roomTypesObj){
        				signUpRooms.push({
        					"version": 0,
        					"roomType":roomTypesObj[key]
        				});
        			}
        		}
        		
        		
        		var checkInDate = form.getValue("checkInDate");
        		var checkOutDate = form.getValue("checkOutDate");
        		var nonMemberType = scatterCheckInBiz.nonMemberTypeS(tempGridRowData,this);
        		
        		if(!tempGridRowData){
        			tempGridRowData = {
        				nonMemGroupReserve:{
        					signUpRooms:signUpRooms
        				},
        				roomPersonalInfos:editGridData
        			};
        		}else{
        			tempGridRowData.nonMemGroupReserve.signUpRooms = signUpRooms;
        			tempGridRowData.roomPersonalInfos = editGridData;
        		}
        		
        		this.set("tempGridRowData",tempGridRowData);
        		
        		$(".J-forms").empty();
        		this.handler.grid.queryRoomTypePrice(this,checkInDate,checkOutDate,pkRoomTypeIn.join(","),signUpRooms,nonMemberType,pkOrgBuildingIn.join(","))
        	}
        	
        	this.roomStateReturnClick(this.get("showForm"),this.get("hideForm"));
        }
    };
});